import java.util.Arrays;
import java.util.Scanner;

public class day12_26 {
    public static void main(String[] args) {
        int [][] arr1 = new int[2][];
        //因为赋值只有一次机会，所以不能这样
        //arr1[0] = {1,3,5};
        arr1[0] = new int[]{1,3,5};
        arr1[1] = new int[]{1,2,3,4,5};

        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr1[i].length; j++) {
                System.out.print(arr1[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main20(String[] args) {
        int[][] arr1 = {{1,2,3}, {4,5,6}};

        //遍历打印
        /*for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(arr1[i][j] + " ");
            }
            System.out.println();
        }*/
        System.out.println(arr1.length);

        System.out.println(arr1[0].length);
        System.out.println(arr1[1].length);
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr1[i].length; j++) {
                System.out.print(arr1[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println(Arrays.deepToString(arr1));

        //利用增强for循环打印
        for(int[] tmp : arr1){
            for (int x : tmp){
                System.out.print(x + " ");
            }
            System.out.println();
        }
    }

    public static void main19(String[] args) {
        int[][] arr1 = {{1,2,3}, {4,5,6}};
        int[][] arr2 = new int[][]{{1,2,3}, {4,5,6}};
        int[][] arr3 = new int[2][3];

        //int[][] arr0 = new int[2][3]{{1,2,3}, {4,5,6}};
        //也不能这样定义
        //int[][] arr4 = new int[][3];
        //java中不能省略行

        int[][] arr5 = new int[2][];
        //可以省略列，这是不规则二维数组

    }

    public static void main18(String[] args) {
        int[] arr = {1,2,3,5};
        int[] copy = Arrays.copyOfRange(arr,0,3);
        System.out.println(Arrays.toString(copy));

    }

    public static void main17(String[] args) {
        int[] arr1 = {2,4,5,3};
        int[] arr2 = new int[arr1.length];//实现数组的拷贝

        int[] copy = Arrays.copyOf(arr1, arr1.length * 2);//返回的是数组
        System.out.println(Arrays.toString(copy));
    }

    public static void main16(String[] args) {
        int[] arr1 = new int[10];
        int[] arr2 = new int[10];

        Arrays.fill(arr1,9);//整体赋值
        System.out.println(Arrays.toString(arr1));

        Arrays.fill(arr2,0,6,9);//区间赋值
        //[)左闭右开
        System.out.println(Arrays.toString(arr2));
    }

    public static void main15(String[] args) {
        int[] arr = {1,2,34,3,4,5,7,23,12};
        System.out.println(Judge(arr));
    }
    public static boolean Judge(int[] arr){
        //利用滑动窗口
        if (arr.length <= 2){
            return false;
        }
        int left = 0, right = 2;
        while(right < arr.length){
            int tmp = left + 1;
            if (arr[left] % 2 == 1 && arr[tmp] % 2 == 1 && arr[right] % 2 == 1){
                return true;
            }
            left++;
            right++;
        }
        return false;
    }

    public static void main14(String[] args) {
        //直接利用哈希表
        //使用相对映射
        int[] arr = {3,3,2,1,4,4,4,4,6,6,6,6,6,6,6};
        int max = 0, min = 0;
        int ret = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max){
                max = arr[i];
            }
            if (arr[i] < min){
                min = arr[i];
            }
        }
        int range = max - min + 1;
        int[] tmp = new int[range];
        for (int i = 0; i < arr.length; i++) {
            tmp[arr[i] - min]++;
        }

        max = 0;//重置max并找tmp中最大值
        ret = 0;
        for (int i = 0; i < range; i++) {
            if (tmp[i] > max){
                max = tmp[i];
                ret = i;//记录下标
            }
        }
        ret += min;

        System.out.println(ret);
    }

    public static void main13(String[] args) {
        int[] arr = {2,2,1};
        int ret = 0;
        //全部异或一遍
        //0异或任何数为它本身
        for (int i = 0; i < arr.length; i++) {
            ret ^= arr[i];
        }
        System.out.println("只出现一次的元素是：" + ret);
    }

    public static void main12(String[] args) {
        int[] arr = {2,7,11,15};
        int target = 9;
        int[] ret = new int[2];

        Findhaha(arr, target, ret);

        for (int i = 0; i < ret.length; i++) {
            System.out.print(ret[i] + " ");
        }
    }
    public static void Findhaha(int[] arr, int target, int[] ret){
        ret[0] = ret[1] = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] + arr[j] == target){
                    ret[0] = i;
                    ret[1] = j;
                }
            }
        }

    }

    public static void main11(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6};

        //Arrays.binarySearch是二分查找
        System.out.println(Arrays.binarySearch(arr, 10));
        //返回的是下标
        //没有找到会返回负数（具体细节看源码）

        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();
        int ret = 0;
        ret = Find(arr, input);




        if (ret == -1){
            System.out.println("没找到");
        }else{
            System.out.println("找到了"+ ret);
        }
    }
    public static int Find(int[] arr, int input){
        int left = 0, right = arr.length - 1;
        int ret = -1;
        while(left <= right){
            int mid = (left + right) >> 1;
            if (input > arr[mid]){
                left = mid + 1;
            }else if (input < arr[mid]){
                right = mid - 1;
            }else{
                ret = arr[mid];
                break;
            }
        }

        return ret;
    }


    public static void main10(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        Adjust(arr);
        for (int x : arr){
            System.out.print(x + " ");
        }
    }
    public static void Adjust(int[] arr){
        int i = 0, j = arr.length - 1;
        while(i < j){
            //前面找偶数
            while(arr[i] % 2 != 0){
                i++;
            }
            //后面找奇数
            while(arr[j] % 2 == 0){
                j--;
            }
            //交换
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
            i++;
            j--;
        }
    }

    public static void main9(String[] args) {
        int[] arr = {1,2,3};
        System.out.println(avg(arr));
    }

    public static int avg(int[] arr){
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum / arr.length;
    }

    public static void main8(String[] args) {
        int[] arr = {1,2,3};
        System.out.println(Sum(arr));
    }
    public static int Sum(int[] arr){
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum;
    }
    
    public static void main7(String[] args) {
        int[] arr = {1,2,3};
        transform(arr);
        for (int x : arr){
            System.out.print(x + " ");
        }
    }
    public static void transform(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            arr[i] *= 2;
        }
    }

    public static void main6(String[] args) {
        int[] arr = new int[100];
        for (int i = 0; i < 100; i++) {
            arr[i] = i + 1;
        }

        for (int i = 0; i < 100; i++) {
            System.out.print(arr[i] + " ");
        }
    }

    public static void main5(String[] args) {

        Hanoi(1,'A','B','C');
        System.out.println();
        Hanoi(2,'A','B','C');
        System.out.println();
        Hanoi(3,'A','B','C');
    }

    public static void move(char pos1, char pos2) {
        System.out.print(pos1 + "->" + pos2 + " ");
    }

    public static void Hanoi(int n, char pos1, char pos2, char pos3) {
        if (n == 1) {
            move(pos1, pos3);
        } else {
            //把 n - 1 个盘子通过中转站 pos3 放在 pos2上
            Hanoi(n - 1, pos1, pos3, pos2);

            //把剩余的1个盘子放在pos3上
            move(pos1, pos3);

            //把 n - 1 个盘子通过中转站 pos1 放在 pos3上
            Hanoi(n - 1, pos2, pos1, pos3);
        }

    }


    public static void main4(String[] args) {
        int[] arr = {1,2,3,9,5,-1};
        //String s = Arrays.toString(arr);
        System.out.println(MyToSting(arr));
        Sort(arr);
        for (int x : arr){
            System.out.print(x + " ");
        }

    }

    public static void Sort(int[] arr){
        for (int i = 0; i < arr.length - 1; i++) {
            //当有一次一趟比较下来都没有交换
            //则相当于已经有序了，则不用在执行，跳出循环即可
            boolean r = false;
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]){
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                    r = true;
                }
            }
            if (r == false){
                break;
            }
        }
    }
    public static String MyToSting(int[] arr){
        if (arr == null){//为空
            return "null";
        }
        if (arr.length == 0){//没有元素
            return "[]";
        }

        String ret = "[";
        for (int i = 0; i < arr.length; i++){
            //拼接
            ret += arr[i];
            if (i != arr.length - 1){
                ret += " ,";
            }
        }

        ret += "]";
        return ret;
    }

    public static void main3(String[] args) {
        int[] ret = func3();
        for (int x : ret){
            System.out.println(x);
        }
    }

    public static int[] func3(){
        int[] ret = new int[2];
        ret[0] = 99;
        ret[1] = 199;
        return ret;
    }

    public static void main2(String[] args) {
        int[] arr = {1,2,3,4};

        func2(arr);

        for (int x : arr){
            System.out.print(x + " ");
        }
        System.out.println();
    }
    public static void func1(int[] arr){
        arr[0] = 99;
    }

    public static void func2(int[] arr){
        arr = new int[]{11,22,33,44,55};
    }


    public static void main1(String[] args) {
        int[] arr = null;
        int[] arr1 = {1,2,3,4};
        int[] arr2 = arr1;
        System.out.println(arr.length);
    }
}
